package com.syl.ippool.poolimpl;

import com.syl.ippool.domain.IpBasePojo;
import com.syl.ippool.ifice.pool.IGraspIpPool;

import java.util.ArrayList;
import java.util.List;

/**
 * set ip池， 顶层抽象类
 */
public abstract class  AbsSetBaseGraspPool implements IGraspIpPool {

    @Override
    public void add(IpBasePojo ipBasePojo) {
        GraspIpPoolEntity.setIps.add(ipBasePojo);
    }

    @Override
    public void add(List<IpBasePojo> ipBasePojos) {
        if (ipBasePojos != null) {
            GraspIpPoolEntity.setIps.addAll(ipBasePojos);
        }
    }

    @Override
    public IpBasePojo get() {
        for (IpBasePojo ip:  GraspIpPoolEntity.setIps){
            return ip;
        }
        return null;
    }

    @Override
    public List<IpBasePojo> get(int num) {
        ArrayList<IpBasePojo> list = new ArrayList<>();
        for (IpBasePojo ip:  GraspIpPoolEntity.setIps){
            list.add(ip);
            if (list.size() >= num) {
                break;
            }
        }
        return list;
    }

    @Override
    public List<IpBasePojo> removeGet(int num) {
        ArrayList<IpBasePojo> list = new ArrayList<>();
        for (IpBasePojo ip:  GraspIpPoolEntity.setIps){
            list.add(ip);
            if (list.size() >= num) {
                break;
            }
        }
        for (IpBasePojo ipBasePojo : list) {
            GraspIpPoolEntity.setIps.remove(ipBasePojo);
        }
        return list;
    }

    @Override
    public boolean remove(IpBasePojo ipBasePojo) {
        boolean remove = GraspIpPoolEntity.setIps.remove(ipBasePojo);
        return remove;
    }

    @Override
    public int getPoolSize() {
        return GraspIpPoolEntity.setIps.size();
    }
}
